package com.liqiye.paymanager.controller;

import com.liqiye.paymanager.bean.payment.*;
import com.liqiye.paymanager.service.payment.StripePaymentService;
import com.stripe.exception.StripeException;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * Stripe支付的接口
 */
@RestController
@RequestMapping("/stripe")
public class StripeController {

    private final StripePaymentService service;

    public StripeController(StripePaymentService service) {
        this.service = service;
    }

    /**
     * 单次付款
     * 创建 intent （意图）
     */
    @PostMapping("/intent/create")
    public PaymentResponse create(@RequestBody PaymentRequest request) {
        return service.createPayment(request);
    }

    /**
     * 退款
     */
    @PostMapping("/refund")
    public RefundResponse refund(@RequestBody RefundRequest req) {
        return service.refund(req);
    }

    /**
     * 创建客户
     * 如果是【重复付款】或者【订阅付款】这种类型的，必须基于 Customer 来付款
     * 创建 Customer --> 将 PaymentMethod 绑定到该 customer --> 创建订阅（Subscription）或用 customer 扣费
     */
    @PostMapping("/customer/create")
    public ResponseEntity<CustomerResponse> createCustomer(@RequestBody CreateCustomerRequest req) {
        CustomerResponse customer = service.createCustomer(req);
        return ResponseEntity.ok(customer);
    }

    /**
     * 记录 stripe 的扣款记录 charge
     */
    @PostMapping("/charge/record")
    public ResponseEntity<?> recordCharge(@RequestParam("paymentIntentId")String paymentIntentId) {
        service.recordCharge(paymentIntentId);
        return ResponseEntity.ok("success");
    }


    // ================================ 自动付款 ===================================


    /**
     * 自动付款 请求创建
     * 请求后端创建 SetupIntent + Customer
     */
    @PostMapping("/setup/intent/create")
    public PaymentResponse createSetupIntent(@RequestBody CreateSetupIntentRequest request) {
        return service.createSetupIntent(request);
    }

    /**
     * 创建订阅
     * Stripe 如何自动扣款?
     * 创建 Subscription 成功后，Stripe 会根据 Price 的周期定时自动生成 invoice。Stripe 会自动尝试用绑定的 paymentMethod 扣款。
     */
    @PostMapping("/subscription/create")
    public ResponseEntity createSubscription(@RequestBody CreateSubscriptionRequest request) {
        service.createSubscription(request);
        return ResponseEntity.ok("success");
    }

    /**
     * 取消订阅
     * @param subscriptionId
     * @return
     */
    @PostMapping("/subscription/cancel")
    public ResponseEntity cancelSubscription(@RequestParam String subscriptionId) {
        service.cancelSubscription(subscriptionId);
        return ResponseEntity.ok("success");
    }

    // ============================================================================


    /**
     * webhook 接口
     */
    @PostMapping("/webhook")
    public ResponseEntity<String> handleWebhook(HttpServletRequest request) {
        return service.handleWebhook(request);
    }

    /**
     * 获取 stripe 商户账号余额
     */
    @GetMapping("/balance")
    public Map<String, Object> getBalance() {
        try {
            return service.queryBalance();
        } catch (StripeException e) {
            return Map.of("error", e.getMessage());
        }
    }

    /**
     * 提现（stripe 账户的钱提现到 绑定给得银行卡）
     */
    @PostMapping("/withdraw")
    public ResponseEntity withdraw(@RequestBody WithdrawRequest request) throws StripeException {
        service.withdraw(request);
        return ResponseEntity.ok("success");
    }


}
